<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.min.css">
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.2/js/bootstrap.min.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User List</title>
</head>
<body>

<div class="container mt-5">
    <h2>用户列表</h2>

    <table id="user-table" class="table table-striped">
        <thead>
        <tr>
            <th>ID</th>
            <th>Username</th>
            <th>Email</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        </tbody>
    </table>

    <nav aria-label="User navigation">
        <ul id="pagination" class="pagination justify-content-center">
        </ul>
    </nav>

</div>


<script>
    $(document).ready(function () {
        // 定义一个函数来发送Ajax请求获取用户列表数据
        function getUserList(page) {
            $.ajax({
                url: '/api/user/list',  // 后端接口的URL
                type: 'GET',
                data: {page: page},  // 可选参数：页码
                dataType: 'json',
                success: function (response) {
                    // 清空用户表格的tbody
                    $('#user-table tbody').empty();

                    // 遍历响应数据中的每个用户，并将其添加到用户表格的tbody中
                    response.forEach(function (user) {
                        var row = '<tr>' +
                            '<td>' + user.id + '</td>' +
                            '<td>' + user.username + '</td>' +
                            '<td>' + user.email + '</td>' +
                            '<td>查询</td>' +
                            '</tr>';
                        $('#user-table tbody').append(row);
                    });

                    // 更新分页导航栏
                    updatePagination(response.page, response.total_pages);
                },
                error: function (xhr, status, error) {
                    console.log('Error:', error);
                }
            });
        }

        // 定义一个函数来更新分页导航栏
        function updatePagination(currentPage, totalPages) {
            var pagination = '';

            // 添加“Previous”按钮
            if (currentPage > 1) {
                pagination += '<li class="page-item">' +
                    '<a class="page-link" href="#" onclick="getUserList(' + (currentPage - 1) + ')">Previous</a>' +
                    '</li>';
            } else {
                pagination += '<li class="page-item disabled">' +
                    '<a class="page-link" href="#" tabindex="-1" aria-disabled="true">Previous</a>' +
                    '</li>';
            }

            // 添加每个页码按钮
            for (var i = 1; i <= totalPages; i++) {
                if (i === currentPage) {
                    pagination += '<li class="page-item active" aria-current="page">' +
                        '<a class="page-link" href="#" onclick="getUserList(' + i + ')">' + i + '<span class="sr-only">(current)</span></a>' +
                        '</li>';
                } else {
                    pagination += '<li class="page-item">' +
                        '<a class="page-link" href="#" onclick="getUserList(' + i + ')">' + i + '</a>' +
                        '</li>';
                }
            }

            // 添加“Next”按钮
            if (currentPage < totalPages) {
                pagination += '<li class="page-item">' +
                    '<a class="page-link" href="#" onclick="getUserList(' + (currentPage + 1) + ')">Next</a>' +
                    '</li>';
            } else {
                pagination += '<li class="page-item disabled">' +
                    '<a class="page-link" href="#" tabindex="-1" aria-disabled="true">Next</a>' +
                    '</li>';
            }

            // 将生成的分页导航栏HTML代码添加到页面中
            $('#pagination').html(pagination);
        }

        // 初始化页面加载第一页的用户列表
        getUserList(1);
    });
</script>
</body>
</html>
